package share.download.utils;

import java.util.HashMap;

public class ClientIpTable {
	private static HashMap<String, Integer> table=new HashMap<String, Integer>();
	
	/**
	 * 注册一个新的IP,并将当前值+1
	 * @return 如果原来不存在,返回0,如果存在返回返回当前值.
	 */
	public synchronized static int registerIP(String ip){
		if(!table.containsKey(ip)){
			table.put(ip, 1);
			return 0;
		}
		Integer count=table.get(ip)+1;
		table.put(ip, count);
		return count;
	}
	
	public static synchronized int unregisterIP(String ip){
		if(!table.containsKey(ip))
			return 0;
		Integer count=table.get(ip)-1;
		if(count<=0){
			table.remove(ip);
			return 0;
		}
		else
			table.put(ip, count);
		return count;
	}
}
